//
//  ADLbsSearchManager.m
//  ADUserPro
//
//  Created by AnDa-Y on 2020/8/3.
//  Copyright © 2020 AnDa-Y. All rights reserved.
//

#import "ADLbsSearchManager.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapSearchKit/AMapSearchObj.h>

#import "AMapPOISearchResponse+LocationModel.h"

#define kSManagerSelf  [ADLbsSearchManager sharedManager]

@interface ADLbsSearchManager ()<AMapSearchDelegate>

@property (nonatomic,strong) AMapSearchAPI *searchApi;
@property (nonatomic, strong) NSMapTable *mapTable;

@end

@implementation ADLbsSearchManager

+ (instancetype)sharedManager
{
    static ADLbsSearchManager *manager = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        manager = [[self alloc]init];
        manager.searchApi = [[AMapSearchAPI alloc]init];
        manager.searchApi.delegate = manager;
        manager.mapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsCopyIn];
    });
    return manager;
}

+ (void)reGeoWithCoordinate:(CLLocationCoordinate2D)coor
                 completion:(ADReGeoSearchResultBlock)reGeoResultBlock
{
    AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
    request.requireExtension = YES;
    request.location = [AMapGeoPoint locationWithLatitude:coor.latitude longitude:coor.longitude];
    [kSManagerSelf.searchApi AMapReGoecodeSearch:request];
    [kSManagerSelf.mapTable setObject:reGeoResultBlock forKey:request];
}

+ (void)searchLocationsWithKeyword:(NSString *)keyword city:(NSString *)city completion:(ADLocationSearchResultBlock)searchResultBlock
{
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.requireExtension = YES;
    request.sortrule = 1;
    request.keywords = keyword;
    
    if (city.length > 0){
        request.city = city;
        request.cityLimit = YES;
    }
    [kSManagerSelf.searchApi AMapPOIKeywordsSearch:request];
    [kSManagerSelf.mapTable setObject:searchResultBlock forKey:request];
}

+ (void)searchRouteWithType:(ADRouteSearchType)routeSearchType
                     origin:(CLLocationCoordinate2D)origin
                destination:(CLLocationCoordinate2D)destination
                   complete:(ADRouteSearchResultBlock)searchResultBlock
{
    if (routeSearchType == ADRouteSearchTypeDrive) {
        AMapDrivingRouteSearchRequest *drivingRequest = [[AMapDrivingRouteSearchRequest alloc] init];
        drivingRequest.strategy = 0;
        drivingRequest.origin = [AMapGeoPoint locationWithLatitude:origin.latitude longitude:origin.longitude];
        drivingRequest.destination = [AMapGeoPoint locationWithLatitude:destination.latitude longitude:destination.longitude];
        drivingRequest.requireExtension = NO;
        [kSManagerSelf.searchApi AMapDrivingRouteSearch:drivingRequest];
        [kSManagerSelf.mapTable setObject:searchResultBlock forKey:drivingRequest];
    }else if (routeSearchType == ADRouteSearchTypeWalk){
        AMapWalkingRouteSearchRequest *walkRequest = [[AMapWalkingRouteSearchRequest alloc] init];
        walkRequest.origin = [AMapGeoPoint locationWithLatitude:origin.latitude longitude:origin.longitude];
        walkRequest.destination = [AMapGeoPoint locationWithLatitude:destination.latitude longitude:destination.longitude];
        [kSManagerSelf.searchApi AMapWalkingRouteSearch:walkRequest];
        [kSManagerSelf.mapTable setObject:searchResultBlock forKey:walkRequest];
    }else if (routeSearchType == ADRouteSearchTypeRide){

    }
}

#pragma mark - amapsearch delegate

//POI搜索回调
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    NSArray *locationModels = [((AMapPOISearchResponse *)response) locationModels];
    ADLocationSearchResultBlock block = [self.mapTable objectForKey:request];
    block(locationModels, nil);
    [self.mapTable removeObjectForKey:request];
}

//反地理搜索回调
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    ADLocationModel *locationModel = [ADLocationModel new];
    locationModel.latitude = request.location.latitude;
    locationModel.longitude = request.location.longitude;
    locationModel.adcode = response.regeocode.addressComponent.adcode;
    locationModel.province = response.regeocode.addressComponent.province;
    locationModel.city = response.regeocode.addressComponent.city;
    locationModel.name = response.regeocode.formattedAddress;
    locationModel.address = response.regeocode.formattedAddress;
    
    ADReGeoSearchResultBlock block = [self.mapTable objectForKey:request];
    block(locationModel, nil);
    [self.mapTable removeObjectForKey:request];
}

//路径搜索回调
-  (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    AMapRoute *route = ((AMapRouteSearchResponse *)response).route;
    AMapPath *nowRoute = route.paths.firstObject;
    
    ADRouteSearchResultBlock block = [self.mapTable objectForKey:request];
    block(nowRoute, nil);
    [self.mapTable removeObjectForKey:request];
}

//搜索失败回调
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
    void (^block)(id result, NSError *error) = [self.mapTable objectForKey:request];
    block(nil, error);
    [self.mapTable removeObjectForKey:request];
}

@end
